package org.monklypse.jme2.demoapp.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.monklypse.core.JMEComposite;

import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.WireframeState;
import com.jme.system.canvas.JMECanvasImplementor;
import com.jme.system.canvas.SimpleCanvasImpl;

public class JMEStatesComposite extends JMEComposite {

	private Combo list;

	public JMEStatesComposite(Composite parent, JMECanvasImplementor implementor) {
		super(parent, implementor);
	}

	protected void createControls() {
		setLayout(new GridLayout(1, false));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		buildRenderStateList(composite);
		Button button = new Button(composite, SWT.NONE);
		button.setText("Apply state:");
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				applyState();
			}
		});

		button = new Button(composite, SWT.NONE);
		button.setText("Reset:");
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				resetStates();
			}
		});

		initializeJMEComponents();

		new SimpleStatesComposite(this, implementor);
	}

	private void buildRenderStateList(Composite parent) {
		list = new Combo(parent, SWT.SINGLE | SWT.READ_ONLY);
		list.add("Material");
		list.add("Alpha");
		list.add("WireFrame");
	}

	private void resetStates() {
		Node node = ((SimpleCanvasImpl) implementor).getRootNode();
		int stateNo = RenderState.RS_MAX_STATE;
		for (int i = 0; i < stateNo; i++) {
			node.clearRenderState(i);
		}
		((SimpleCanvasImpl) implementor).getRootNode().setRenderQueueMode(
				Renderer.QUEUE_OPAQUE);
		node.updateRenderState();
		node.updateGeometricState(0, false);
	}

	private void applyState() {
		if (list.getSelectionIndex() == -1)
			return;
		String item = list.getItem(list.getSelectionIndex());
		if (item.equalsIgnoreCase("Material")) {
			MaterialState state = implementor.getRenderer()
					.createMaterialState();
			state.setEnabled(true);
			state.setAmbient(ColorRGBA.red);
			state.setEmissive(ColorRGBA.blue);
			state.setDiffuse(ColorRGBA.yellow);
			state.setSpecular(ColorRGBA.brown);
			state.setShininess(120);
			((SimpleCanvasImpl) implementor).getRootNode()
					.setRenderState(state);
			((SimpleCanvasImpl) implementor).getRootNode().updateRenderState();
		} else if (item.equalsIgnoreCase("Alpha")) {
			BlendState state = implementor.getRenderer().createBlendState();
			state.setEnabled(true);
			state.setBlendEnabled(true);
			state.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
			state
					.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
			((SimpleCanvasImpl) implementor).getRootNode().setRenderQueueMode(
					Renderer.QUEUE_TRANSPARENT);
			((SimpleCanvasImpl) implementor).getRootNode()
					.setRenderState(state);
			((SimpleCanvasImpl) implementor).getRootNode().updateRenderState();
		} else if (item.equalsIgnoreCase("WireFrame")) {
			WireframeState state = implementor.getRenderer()
					.createWireframeState();
			state.setEnabled(true);
			((SimpleCanvasImpl) implementor).getRootNode()
					.setRenderState(state);
			((SimpleCanvasImpl) implementor).getRootNode().updateRenderState();
		}
	}
}
